package jschool.ui.servidor.paneles;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Spring;
import javax.swing.SpringLayout;
import javax.swing.border.LineBorder;

import jschool.controladores.servidor.ControladorTeclasServidor;
import jschool.ui.imagenes.GestorImagen;

public class PanelLogin extends JPanel implements Runnable{

	private static final long serialVersionUID = 1L;
	
	private Thread hiloAnimacion;
	private JLabel animacion;
	private GestorImagen gestorImagen;
	
	private JLabel usuarioL;
	private JLabel claveL;
	private Font fuenteEtiquetas;
	
	private ControladorTeclasServidor controladorTeclasServidor;
	
	private JTextField usuarioT;
	private JPasswordField claveT;
	
	private JPanel decoracion;
	private JLabel eslogan;
	private Font fuenteEslogan;
	
	private SpringLayout layout;
	
	
	public PanelLogin() {
		
		layout = new SpringLayout();
		
		setVisible( true );
		setLayout( layout );
		setPreferredSize( new Dimension( 500, 310 ) );
		setBorder( new LineBorder( Color.BLACK, 2 ) ); 
		setBackground( Color.WHITE );
		
		fuenteEtiquetas = new Font( Font.MONOSPACED, Font.PLAIN, 20 );
		
		usuarioL = new JLabel( "Usuario: " );
		usuarioL.setFont( fuenteEtiquetas );
		
		claveL = new JLabel( "Clave: " );
		claveL.setFont( fuenteEtiquetas );
		
		controladorTeclasServidor = ControladorTeclasServidor.getInstancia();
		
		usuarioT = new JTextField( 15 );
		usuarioT.setName( "loguear" );
		usuarioT.addKeyListener( controladorTeclasServidor );
		
		claveT = new JPasswordField( 15 );
		claveT.setName( "loguear" );
		claveT.addKeyListener( controladorTeclasServidor );
		
		hiloAnimacion = new Thread( this );
		animacion = new JLabel();
		gestorImagen = GestorImagen.getInstancia();
		
		decoracion = new JPanel();
		decoracion.setPreferredSize( new Dimension( 496, 50 ) );
		decoracion.setVisible( true );
		decoracion.setBackground( Color.BLACK );
		decoracion.setBorder( new LineBorder( Color.WHITE, 2 ) );
		
		fuenteEslogan = new Font( Font.SANS_SERIF, Font.BOLD, 30 );
		
		eslogan = new JLabel( "::: JSchool :::", JLabel.CENTER );
		eslogan.setFont( fuenteEslogan );
		eslogan.setForeground( Color.WHITE );
		
		decoracion.add( eslogan );
		
		layout.putConstraint( SpringLayout.NORTH, decoracion, Spring.constant( 0 ), 
				SpringLayout.NORTH, this );
		
		add( decoracion );
		
		layout.putConstraint( SpringLayout.SOUTH, usuarioL, Spring.constant( 1 ), 
				SpringLayout.SOUTH, usuarioT );
		
		layout.putConstraint( SpringLayout.WEST, usuarioL, Spring.constant( 80 ), 
				SpringLayout.WEST, this );
		
		layout.putConstraint( SpringLayout.NORTH, usuarioT, Spring.constant( 60 ), 
				SpringLayout.SOUTH, decoracion );
		
		layout.putConstraint( SpringLayout.WEST, usuarioT, Spring.constant( 5 ), 
				SpringLayout.EAST, usuarioL );
		
		layout.putConstraint( SpringLayout.SOUTH, claveL, Spring.constant( 1 ), 
				SpringLayout.SOUTH, claveT );
		
		layout.putConstraint( SpringLayout.WEST, claveL, Spring.constant( 0 ), 
				SpringLayout.WEST, usuarioL );
		
		layout.putConstraint( SpringLayout.NORTH, claveT, Spring.constant( 40 ), 
				SpringLayout.SOUTH, usuarioT );
		
		layout.putConstraint( SpringLayout.EAST, claveT, Spring.constant( 0 ), 
				SpringLayout.EAST, usuarioT );
		
		layout.putConstraint( SpringLayout.NORTH, animacion, Spring.constant( 70 ), 
				SpringLayout.SOUTH, usuarioT );
		
		layout.putConstraint( SpringLayout.WEST, animacion, Spring.constant( 20 ), 
				SpringLayout.WEST, usuarioT );
		
		
		add( usuarioL );
		add( usuarioT );
		add( claveL );
		add( claveT );
		
		add( animacion );
		
		hiloAnimacion.start();
	}

	
	public String getUsuario() {
		return usuarioT.getText();
	}
	
	public String getClave() {
		return String.copyValueOf( claveT.getPassword() );
	}
	
	public void run() {
		
		int indice = 0;
		
		while( true ) {
			animacion.setIcon( gestorImagen.getImagenAnimacion( indice++ ) );
			
			if( indice > 18 ) {
				indice = 0;
			}
			
			try {
				Thread.sleep( 80 );
			}
			catch( InterruptedException ie ) {
				ie.printStackTrace();
			}
		}
	}
}
